package com.fubowen.lepton.ui.reader.recycler

import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.fubowen.reader.components.SlideControlLayoutManager
import com.fubowen.lepton.databinding.FragmentReaderItemBinding
import com.fubowen.lepton.databinding.FragmentReaderSelectionBinding
import com.fubowen.lepton.helper.ViewHelper.createPopupWindow
import com.fubowen.lepton.helper.ViewHelper.dpToPx
import com.fubowen.lepton.helper.ViewHelper.setShapeColor
import com.fubowen.lepton.helper.ViewHelper.setThemeColor
import com.fubowen.lepton.helper.ViewHelper.setVisible
import com.fubowen.lepton.theme.ThemeViewModel

class BookChapterAdapter(
    private val layoutManager: SlideControlLayoutManager,
    private val context: Context,
    private val viewModel: ThemeViewModel
) : PagingDataAdapter<BookChapter, BookChapterAdapter.ViewHolder>(DiffCallback()) {

    private var onClickListener: OnClickListener? = null
    private val selectionMenu = context.createPopupWindow() { inflate, _ ->
        FragmentReaderSelectionBinding.inflate(inflate).let {
            it.selectionAction.setShapeColor(Color.parseColor("#303030"))
            it.root
        }
    }

    //region onCreateViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            FragmentReaderItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }
    //endregion

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        if (item != null) {
            holder.name.apply {
                text = String.format("第%d章 %s", item.order, item.name)
                setTextSize(TypedValue.COMPLEX_UNIT_PX, item.headerTextSize)
                setLineSpacing(item.headerLineSpacingExtra, item.headerLineSpacingMultiplier)
                setVisible(item.isPageStart)
            }
            holder.content.apply {
                text = item.content
                setTextSize(TypedValue.COMPLEX_UNIT_PX, item.textSize)
                setLineSpacing(item.lineSpacingExtra, item.lineSpacingMultiplier)
            }
            holder.itemView.setThemeColor(viewModel.getBackgroundColor(), viewModel.getTextColor())
            holder.loading.setVisible(item.isLoading)
        }
    }

    //region setOnClickListener
    fun setOnClickListener(listener: OnClickListener) {
        onClickListener = listener
    }
    //endregion

    //region ViewHolder
    inner class ViewHolder(
        binding: FragmentReaderItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        val content = binding.chapterContent
        val name: TextView = binding.chapterName
        val loading = binding.chapterLoading

        init {
            binding.root.setOnClickListener { onClickListener?.onItemClick(binding.root) }
            SelectionHelper(content).apply {
                onShow = { x, y ->
                    layoutManager.enableY = false
                    layoutManager.enableX = false
                    val mx = x.toInt()
                    val my = y.toInt() + context.dpToPx(8)
                    if (selectionMenu.value.isShowing) {
                        selectionMenu.value.update(content, mx, my, -1, -1)
                    } else {
                        selectionMenu.value.showAsDropDown(content, mx, my)
                    }
                }
                onHide = {
                    layoutManager.enableY = true
                    layoutManager.enableX = true
                    selectionMenu.value.dismiss()
                }
                attachTo()
            }
        }
    }
    //endregion

    //region DiffCallback
    class DiffCallback : DiffUtil.ItemCallback<BookChapter>() {
        override fun areItemsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean {
            return oldItem == newItem
        }
    }
    //endregion

    //region OnClickListener
    interface OnClickListener {
        fun onItemClick(view: View?)
    }
    //endregion
}